# -*- coding: utf-8 -*-

from db.mysql import MysqlClient
import json
from common.config import *


class Spider(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "“乐行人生”交通工具意外伤害保险"
        self.table = "accident"

    def selectData(self, data={}):
        SQL = "SELECT * FROM `" + self.name + "`"

        self.mysql.cursor.execute(SQL)

        result = self.mysql.cursor.fetchall()

        for item in result:
            result = json.loads(item["plan"])

            data["name"] = self.name
            data["company"] = "太平洋保险"
            data["days"] = "1d"
            data["people_count"] = "1"
            data["airplane_death"] = result["乘坐客运民航班机"].replace("w", "0000")
            data["passenger_death"] = result["乘坐客运机动车"].replace("w", "0000")
            data["ship_disability"] = result["乘坐客运轮船"].replace("w", "0000")
            data["private_death"] = result["乘坐非营运机动车"].replace("w", "0000")
            data["train_death"] = result["乘坐客运轨道交通车辆"].replace("w", "0000")
            data["price"] = item["price"]

            self.mergeTable(data)

    # 合并表格
    def mergeTable(self, data):
        SQL = 'INSERT INTO `' + TABLE_ACCIDENT + '`(`name`,`company`,`days`,`people_count`,`airplane_death`,`passenger_death`,`ship_disability`,`private_death`,`train_death`,`price`)VALUES("{name}","{company}","{days}","{people_count}","{airplane_death}","{passenger_death}","{ship_disability}","{private_death}","{train_death}","{price}")'

        print(SQL.format(**data))
        #
        # self.mysql.cursor.execute(SQL.format(**data))
        # self.mysql._conn.commit()
        #
        # print(data)
